package com.zyy.smartcity1129.hospital;

import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;
import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.data.HospitalBannerInfo;
import com.zyy.smartcity1129.data.HospitalInfo;
import com.zyy.smartcity1129.util.BannerAd;
import com.zyy.smartcity1129.util.Http;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: Hospital1Fragment$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 1/12/2021$
 * @Version: 1.0
 */
public class Hospital1Fragment extends BaseFragment {
    private HospitalInfo.RowsBean hospital = HospitalFragment.hospital;
    private Banner bannerHospital;
    private WebView webViewHospital;
    private Button btnHospital;

    @Override
    protected int layId() {
        return R.layout.f_hospital1;
    }

    @Override
    protected String title() {
        return hospital.getHospitalName();
    }

    @Override
    protected boolean back() {
        return true;
    }

    @Override
    protected Boolean bottom() {
        return false;
    }

    @Override
    protected void init(View iv) {
        initView(iv);
        th(() -> {
            List<HospitalBannerInfo.DataBean> hospitalBannerList = Http.getInstance().get("/prod-api/api/hospital/banner/list", null, HospitalBannerInfo.class, 1).getData();
            List<Object> list = new ArrayList<>();
            for (HospitalBannerInfo.DataBean d : hospitalBannerList) {
                if (d.getHospitalId() == hospital.getId()) {
                    list.add(d.getImgUrl());
                }
            }
            ui(() -> {
                bannerHospital.setAdapter(new BannerAd(list)).addBannerLifecycleObserver(this).setIndicator(new CircleIndicator(getContext())).start();
            });
        });
        webViewHospital.loadUrl(hospital.getBrief());
        webViewHospital.loadDataWithBaseURL(Http.base, hospital.getBrief(), "text/html", "utf-8", null);
        webViewHospital.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
        btnHospital.setOnClickListener(view -> {
            add(new PatientFragment());
        });
    }

    private void initView(View iv) {
        bannerHospital = (Banner) iv.findViewById(R.id.bannerHospital);
        webViewHospital = (WebView) iv.findViewById(R.id.webViewHospital);
        btnHospital = (Button) iv.findViewById(R.id.btnHospital);
    }
}
